package dyyx.util;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;

import javax.management.MBeanServerConnection;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;

import dyyx.dto.WebResult;

public abstract class JmxViewWebUtil {

	private static final Set<String> OPS = new HashSet<>();

	private static final String OP_CONNECT = "connect";
	private static final String OP_DIS_CONNECT = "disConnect";
	private static final String OP_CLIENT_INFO = "clientInfo";
	private static final String OP_QUERY_MBEAN = "queryMbean";
	private static final String OP_QUERY_MBEAN_INFO = "queryMbeanInfo";

	
	private static final String RESULT_TYPE_TEXT = "text";
	private static final String RESULT_TYPE_JSON = "json";



	static {
		

		OPS.add(OP_CONNECT);
		OPS.add(OP_DIS_CONNECT);
		OPS.add(OP_CLIENT_INFO);
		OPS.add(OP_QUERY_MBEAN);
		OPS.add(OP_QUERY_MBEAN_INFO);
		

	}
	
	

	public static void run(HttpServletRequest request, WebResult webResult) throws Exception {
		try {
			run_(request, webResult);
		} catch (Throwable e) {
			webResult.msg = webResult.op + " error," + e;
		}
	}
	
	

	private static void run_(HttpServletRequest request, WebResult webResult) throws Exception {
		String op = WebUtil.get(request, "op");
		String connstr = WebUtil.get(request, "connstr");
		String 	q = WebUtil.get(request, "q");
		String resultType = WebUtil.get(request, "resultType");
		

		if (StringUtils.isBlank(op)) {
			webResult.msg = "op blank";
			return;
		}
		if (!OPS.contains(op)) {
			webResult.msg = "op error";
			return;
		}
		webResult.op = op;

		if (OP_CONNECT.equals(op)) {

			if (StringUtils.isBlank(connstr)) {
				webResult.msg = "connstr blank";
				return;
			}

			JmxClientUtil.connect(connstr);

			webResult.msg = op + " success,connstr=" + connstr;

			return;
		}

		if (OP_DIS_CONNECT.equals(op)) {

			JmxClientUtil.disconnect();

			webResult.msg = op + " success";

			return;
		}

		if (OP_CLIENT_INFO.equals(op)) {

			Map<String, Object> map = JmxClientUtil.getClientInfo();
			
			
			webResult.msg = map.toString() ;

			return;
		}

		//

		if (OP_QUERY_MBEAN.equals(op)) {

			MBeanServerConnection conn = JmxClientUtil.getClient().mbeanServerConnection;
			
			Set<String> names = JmxUtil.queryAsString(conn,null);
			
			if(names==null){
				names = new TreeSet<>();
				
			}
			names = JmxUtil.matchedSet(names, q);
			
			List<String> list = new ArrayList<>();
			
			list.addAll(names);
			webResult.mbeanNameList = list;

			return;
		}

		if (OP_QUERY_MBEAN_INFO.equals(op)) {

            MBeanServerConnection conn = JmxClientUtil.getClient().mbeanServerConnection;
			
			Set<String> names = JmxUtil.queryAsString(conn,null);
			
			if(names==null){
				names = new TreeSet<>();
				
			}
			names = JmxUtil.matchedSet(names, q);
			Map<String,Map<String,Object>> map = JmxUtil.getMBeanAttributeValuesMap(conn, names);
			
			webResult.mbeanInfoMap = map;
			
			return;
		}
		
		throw new Exception("op error,"+op);


	}
	
	

}
